<script lang="ts" setup>
import { ref, reactive } from "vue";
import { FormInstance, FormRules, FormItemRule } from "element-plus";
import { verifyMobile, verifyPWD } from "@/utils/verify";
import mainEmit from "@/utils/bus";
import { LoginApi, CommonApi } from "@/Api";

const formRef = ref<FormInstance>();
const sendLoading = ref(false);
const sendText = ref("发送验证码");
const sendDisabled = ref(false);

const formData = reactive({
   account: "",
   phone: "",
   pwd: "",
   verifyPwd: "",
   smgId: "",
   code: ""
});

// 重复密码验证
const validatePass: FormItemRule["validator"] = (rule, value, callback) => {
   if (value == "") return callback("请再次输入密码");
   if (value != formData.pwd) return callback("与原密码不相同");
   callback();
};

const rules = reactive<FormRules>({
   account: [
      { required: true, message: "请输入账号", trigger: "blur" },
      { min: 5, max: 20, message: "账号长度在 5 - 20 之间", trigger: "blur" }
   ],
   phone: [
      { required: true, message: "请输入手机号码", trigger: "blur" },
      { pattern: verifyMobile, message: "手机号码格式错误", trigger: "blur" }
   ],
   code: [
      { required: true, message: "请输入验证码", trigger: "blur" },
      { pattern: /[0-9]{6}/, message: "验证码为六位数字", trigger: "blur" }
   ],
   pwd: [
      { required: true, message: "请输入密码", trigger: "blur" },
      { pattern: verifyPWD, message: "密码为数字和字母组合，且长度在8-16之间", trigger: "blur" }
   ],
   verifyPwd: [
      { required: true, message: "请再次输入密码", trigger: "blur" },
      { validator: validatePass, trigger: "blur" }
   ]
});

/** 发送短信 */
const sendSmg = async () => {
   if (formData.phone == "") return ElMessage.error("请输入手机号码");
   if (!verifyMobile.test(formData.phone)) return ElMessage.error("手机号格式错误");

   sendLoading.value = true;
   sendDisabled.value = true;

   const { data, code } = await CommonApi.SendMessage({ phone: formData.phone });
   if (code == 200) formData.smgId = data.smgId;
   sendLoading.value = false;

   let time = 60;
   const timer = setInterval(() => {
      time--;
      sendText.value = time + "秒";
      if (time == 0) {
         clearInterval(timer);
         sendDisabled.value = false;
         sendText.value = "发送验证码";
      }
   }, 1000);
};

// 登录
function submit(formRef?: FormInstance) {
   if (!formRef) return;
   formRef.validate(async (valid, fields) => {
      if (!valid) return;

      const { data, code, msg } = await LoginApi.AdminSignIn(formData);
      if (code === 200) {
         mainEmit.emit("loginResult", data);
      }
   });
}
</script>

<template>
   <el-form ref="formRef" class="elForm" :model="formData" :rules="rules" label-width="auto">
      <el-form-item prop="account">
         <template v-slot:label>
            <span class="label">账号</span>
         </template>
         <el-input v-model="formData.account" placeholder="输入账号" />
      </el-form-item>
      <el-form-item prop="phone">
         <template v-slot:label>
            <span class="label">手机号码</span>
         </template>
         <el-input v-model="formData.phone" placeholder="输入手机号码" />
      </el-form-item>
      <el-form-item prop="code">
         <template v-slot:label>
            <span class="label">验证码</span>
         </template>
         <el-input class="send" v-model="formData.code" placeholder="输入验证码">
            <template #append>
               <el-tooltip :content="sendText" placement="bottom" effect="light">
                  <el-button @click="sendSmg" :loading="sendLoading" :disabled="sendDisabled">
                     <el-icon v-if="!sendLoading"><Promotion /></el-icon>
                  </el-button>
               </el-tooltip>
            </template>
         </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
         <template v-slot:label>
            <span class="label">密码</span>
         </template>
         <el-input v-model="formData.pwd" type="password" placeholder="输入密码" />
      </el-form-item>
      <el-form-item prop="verifyPwd">
         <template v-slot:label>
            <span class="label">重复密码</span>
         </template>
         <el-input v-model="formData.verifyPwd" type="password" placeholder="再次输入密码" />
      </el-form-item>
      <el-form-item class="elButton">
         <el-button @click="submit(formRef)">登录</el-button>
      </el-form-item>
   </el-form>
</template>

<style lang="scss" scoped>
.elForm {
   width: 100%;
   :deep(.el-input) {
      background-color: transparent;
      .el-input__wrapper {
         background-color: rgba($color: #fff, $alpha: 0.1);
         transition: all 0.3s;
         // border: 1px solid rgba($color: #fff, $alpha: 0.5);
         box-shadow: 0 0 0 1px rgba($color: #fff, $alpha: 0.3) inset;

         .el-input__inner {
            background-color: transparent;
            color: #000;
         }
      }
      .is-focus {
         background-color: rgba($color: #fff, $alpha: 0.5);
      }
   }

   :deep(.send) {
      .el-input-group__append {
         width: 20px;
         background-color: rgba($color: #fff, $alpha: 0.1);
         border: 1px solid rgba($color: #fff, $alpha: 0.3);
         border-left: none;
         color: #fff;
         transition: all 0.3s;
         box-shadow: none;

         &:hover {
            background-color: rgba($color: #fff, $alpha: 0.3);
            border: 1px solid rgba($color: #fff, $alpha: 0.5);
            border-left: none;
         }
         .is-loading {
            > span {
               display: none;
            }
            &::before {
               background-color: transparent;
            }
         }
      }
   }
}
.label {
   color: #fff;
}

.elButton {
   :deep(.el-button) {
      margin-top: 30px;
      width: 100%;
      background-color: transparent;
      border: 1px solid rgba($color: #fff, $alpha: 0.3);
      color: #fff;
      transition: all 0.3s;

      &:hover {
         background-color: rgba($color: #fff, $alpha: 0.5);
         color: #000;
      }
   }
}
</style>
